<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="item in navList" :key="item.name" @click="handleClick(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 每一个楼层 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 标题 -->
        <image class="floor-title" :src="item.floor_title.image_src"></image>
        <!-- 内容 -->
        <view class="floor-img-box">
          <!-- 内容的左侧 -->
          <!-- #2 把 view 改成 navigator 进行跳转 -->
          <navigator :url="item.product_list[0].navigator_url" class="left-img-box">
            <image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix">
            </image>
          </navigator>
          <!-- 右侧的结构 -->
          <view class="right-img-box">
            <!-- 右侧结构中每一个小盒子 -->
            <!-- #3 把 view 改成 navigator 进行跳转 -->
            <navigator :url="item2.navigator_url" class="right-img-item" v-for="(item2, index) in item.product_list"
              v-if="index !== 0" :key="index">
              <!-- 小盒子中的图片 -->
              <image :src="item2.image_src" :style="{ width: item2.image_width + 'rpx' }" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import setBadge from '../../mixins/tabbar-badge.js'
  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    mixins: [setBadge],
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg()

        this.swiperList = res.message
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // #1 处理后端返回的地址为我们希望的格式
        res.message.forEach(item => {
          item.product_list.forEach(item2 => {
            item2.navigator_url = '/subpkg/goods_list/goods_list?' + item2.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      handleClick(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },

    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 32rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-title {
    height: 60rpx;
    width: 100%;
  }

  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }

  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .search-box {
    // 设置定位效果为“吸顶”
    // 类似相对定位和固定定位的结合版
    position: sticky;
    // 吸顶的“位置”
    // 当 top 等于 0 这个条件符合的时候，这个 search-box 就固定不动，同时位置也会保留
    top: 0;
    // 提高层级，防止被轮播图覆盖
    z-index: 9;
  }
</style>
